import { xy2expr } from '../../../../x-spreadsheet/src'
import { DateHelper } from '../DateHelper'

export const OrderTable = [
  {
    col: 'orderId',
    primaryKey: true,
    name: '订单编号',
    hidden: true
  },
  {
    col: 'tc_oeb01',
    name: '订单号',
    width: 120,
    editable: true,
    clickable: true,
    textAlign: 'left'
  },
  {
    col: 'tc_oeb02',
    name: '行号',
    width: 40,
    editable: false,
    textAlign: 'center'
  },
  {
    col: 'material',
    name: '成品\n物料',
    editable: true,
    clickable: true,
    textAlign: 'left'
  },
  {
    col: 'itm_name',
    name: '品名',
    width: 160,
    editable: false,
    textAlign: 'left'
  },
  {
    col: 'customer',
    name: '客户',
    editable: true,
    clickable: true,
    textAlign: 'left',
    width: 80
  },
  {
    col: 'customer_name',
    width: 120,
    name: '客户名称',
    editable: false,
    textAlign: 'left'
  },
  {
    col: 'total_qty',
    name: '总订\n单数',
    width: 60,
    popup: true,
    editable: false
  },
  {
    col: 'demand_total',
    name: '总未\n交数',
    width: 60,
    editable: false
  },
  {
    col: 'current_stock',
    name: '当前\n库存',
    width: 60,
    editable: false
  },
  {
    col: 'safe_stock',
    name: '安全\n库存',
    width: 60,
    editable: false
  },
  {
    col: 'lostqty',
    name: '已过期\n排期',
    popup: true,
    width: 60,
    editable: false
  },
  {
    col: 'demand_total',
    name: '未排期\n数量',
    width: 60,
    editable: false,
    valueRender ({
      ri,
      ci,
      value,
      row,
      dateHelper
    }) {
      var cell1 = xy2expr(ci - 5, ri) // 总未交数
      var cell2 = xy2expr(ci + 1, ri) // 30天内排期
      var cell3 = xy2expr(ci + 2, ri) // 30天后排期
      return `=${cell1} - SUM(${cell2},${cell3})`
    }
  },
  {
    col: 'demand_total',
    name: '30天内\n排期',
    width: 60,
    editable: false,
    valueRender ({
      ri,
      ci,
      value,
      row,
      dateHelper
    }) {
      if (!(dateHelper instanceof DateHelper)) return ''
      var column = dateHelper.daysCount
      var start = xy2expr(ci + 2, ri)
      var end = xy2expr(ci + 1 + column, ri)
      return `=SUM(${start}:${end})`
    }
  },
  {
    col: 'demand_total',
    name: '30天后\n排期',
    width: 60,
    editable: false,
    valueRender ({
      ri,
      ci,
      value,
      row,
      dateHelper
    }) {
      if (!(dateHelper instanceof DateHelper)) return ''
      var column = dateHelper.daysCount
      var count = dateHelper.weeksCount
      var start = xy2expr(ci + 1 + column, ri)
      var end = xy2expr(ci + 1 + column + count, ri)
      return `=SUM(${start}:${end})`
    }
  }
]

export const OutOfStockTable = [
  {
    col: 'id',
    name: '唯一编号'
  },
  {
    col: 'date',
    name: '交货日期',
    type: 'date',
    primaryKey: true
  },
  {
    col: 'orderId',
    foreignKey: true,
    name: '订单编号'
  },
  {
    col: 'quantity',
    isValue: true,
    name: '交货数量'
  }
]

export const orders = [
  {
    orderId: 'id00001',
    customer: 'A10003',
    material: 'AA100033000'
  },
  {
    orderId: 'id00002',
    customer: 'A10497',
    material: 'AA104970001'
  }
]

export const stocks = [
  {
    id: 1,
    date: '2021-08-24',
    quantity: 200,
    orderId: 'id00001'
  },
  {
    id: 2,
    date: '2021-08-26',
    quantity: 200,
    orderId: 'id00002'
  }
]
